<?php

namespace Admin\Models\school;

use Admin\Models\BaseModel;
use Helpers\Main;

class XhschoolUserappgroup extends BaseModel
{

    /**
     *
     * @var integer
     */
    public $iSUGId;

    /**
     *
     * @var integer
     */
    public $iUserId;

    /**
     *
     * @var integer
     */
    public $iSAGId;

    /**
     *
     * @var integer
     */
    public $iSchoolId;

    /**
     *
     * @var integer
     */
    public $bDelete;

    /**
     *
     * @var string
     */
    public $tCreateDate;

    /**
     *
     * @var string
     */
    public $iCreateUserId;

    /**
     *
     * @var string
     */
    public $tLastModifyDate;

    /**
     *
     * @var string
     */
    public $iLastModifyUserId;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        parent::initialize();
        $this->setSource("XHSchool_UserAppGroup");
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'XHSchool_UserAppGroup';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhschoolUserappgroup[]
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return XhschoolUserappgroup
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * 根据用户ID删除相关记录
     *
     * @param $idArr
     * @return mixed
     *
     * create by ck 20160930
     */
    public static function deleteRecordById($idArr)
    {
        return self::find(array(
            'conditions' => 'bDelete=0 AND iUserId IN ({idArr:array})',
            'bind'       => ['idArr' => $idArr]
        ))->delete();
    }

    /**
     * 根据学校ID，用户类型，组ID创建sql语句查询不存在组内的学校用户列表
     *
     * @param $schoolId     --学校ID
     * @param $userType     --用户类型
     * @param $groupId      --学校应用组ID
     * @return mixed
     *
     * create by ck 20160930
     */
    public static function createSqlForOutUserByGroupId($schoolId, $userType, $groupId)
    {
        $queryArr['columns'] = ['U.iUserId', 'U.sLogonName', 'U.sUserName', 'U.sSelfPhone', 'U.iUserSex'];
        $queryArr['query']   = "SELECT :columns: FROM XHSys_User AS U LEFT JOIN (SELECT * FROM XHSchool_UserAppGroup WHERE bDelete = 0 AND iSAGId = {$groupId}) AS A ON U.iUserId = A.iUserId WHERE U.iUserType = {$userType} AND U.bDelete = 0 AND IFNULL(A.iSAGId,0) <> {$groupId} AND IFNULL(A.bDelete,0) = 0 AND U.iSchoolId = {$schoolId}";
        return $queryArr;
    }

    /**
     * 批量插入用户到学校用户组
     *
     * @param $userIdArr        --用户ID数组
     * @param $schoolId         --学校ID
     * @param $groupId          --组ID
     * @return bool
     *
     * create by ck 20160930
     */
    public static function batchCreateUserInGroup($userIdArr, $schoolId, $groupId){
        if(count($userIdArr) < 1) return true;
        $manageId = Main::getLoginUserInfo('userId');
        $nowTime = date('Y-m-d H:i:s');
        $addUserSql = 'INSERT INTO XHSchool_UserAppGroup (iUserId,iSAGId,iSchoolId,bDelete,tCreateDate,iCreateUserId,tLastModifyDate,iLastModifyUserId) VALUES ';
        $valueArr = array();
        foreach($userIdArr as $userId){
            $valueArr[] = "({$userId},{$groupId},{$schoolId},0,'{$nowTime}',{$manageId},'{$nowTime}',{$manageId})";
        }
        $addUserSql .= implode(',', $valueArr);
        return \Phalcon\Di::getDefault()->get('db')->execute($addUserSql);
    }

}
